/**
 * @fileOverview 一站网络集团前端公用模块
 * @author Surging
 * @email surging2@qq.com
 * @version 0.1
 * @date 2014/10/8
 * Copyright (c) 2010-2014   一站网络集团
 *
 */
!function (win) {
    /**
     * 全局命名空间
     * @type {{}|*}
     */
    var Z = win.YZW = win.YZW || {};

    /**
     * 版本号
     * @type {string}
     */
    Z.version = '0.1';

    /**
     * 全局 cookie 名称
     * @type {string}
     */
    Z.cookieName = 'mobile';

    /**
     * 全站URL公用类
     * @type {{}}
     */
    Z.domain = {};

    /**
     * 工具类
     * @type {{}}
     */
    Z.util = {};

    /**
     * 全网顶通公共类
     * @type {{}}
     */
    Z.topbar = {};
	
	
}(window);

/**
 * @fileOverview 全站URL公用类
 * @author 老徐 <surging2@qq.com>
 * @version 0.1
 */
!function (Z, win) {

    //用法： window.YZW.domain('domain_help') ==> http://help.shikee.com/

    Z.domain = (function () {
        var _url = win.location.href.split('.')[1],
            url = {
                domain_user: "http://seller." + _url + ".com/",
			   domain_help: "http://help." + _url + ".com/",
                domain_hlzf: "http://www.hulianpay.com/",
                domain_list: "http://list." + _url + ".com/",
                domain_zhs_www: "http://www.zhonghuasuan.com",
                domain_zhs_list: "http://list.zhonghuasuan.com",
                domain_report: "http://report." + _url + ".com/",
                domain_reg: "http://reg." + _url + ".com/",
                domain_login: "http://login." + _url + ".com/",
                domain_www: "http://www." + _url + ".com/",
				domain_sk_www: "http://www.shikee.com"
            };

        return function (name) {
            return url[name];
        };

    })();

}(YZW, window);

/**
 * @fileOverview 工具类
 * @author 老徐 <surging2@qq.com>
 * @version 0.1
 */
!function (Z, $) {
    var win = window,
        doc = win.document,
        util = Z.util;

    /**
     * 去除空格
     * @param str
     * @returns {XML|string|void}
     */
    util.trim = function (str) {
        str = str.replace(/^\s+/, '');
        for (var i = str.length - 1; i >= 0; i--) {
            if (/\S/.test(str.charAt(i))) {
                str = str.substring(0, i + 1);
                break;
            }
        }
        return str;
    };

    /**
     * 判断非空字符
     * @param  {String} val
     * @return {Boolean}
     */
    util.isNotEmptyString = function (val) {
        return (typeof val == 'string') && val !== '';
    };

    /**
     * 获取 Cookie
     * @param  {String} name
     * @return {String}
     */
    util.getCookie = function (name) {
        var m = doc.cookie.match('(?:^|;)\\s*' + name + '=([^;]*)');
        return (m && m[1]) ? decodeURIComponent(m[1]) : '';		
    };

    /**
     * 设置 Cookie
     * @param {String}  name
     * @param {String}  val
     * @param {Number|Date} expires
     * @param {String}  domain
     * @param {String}  path
     * @param {Boolean} secure
     */
    util.setCookie = function (name, val, expires, domain, path, secure) {
        var text = String(encodeURIComponent(val)),
            date = expires;

        // 从当前时间开始，多少天后过期
        if (typeof date === 'number') {
            date = new Date();
            date.setTime(date.getTime() + expires * 24 * 60 * 60 * 1000);
        }

        // expiration date
        if (date instanceof Date) {
            text += '; expires=' + date.toUTCString();
        }

        // domain
        if (this.isNotEmptyString(domain)) {
            text += '; domain=' + domain;
        }

        // path
        if (this.isNotEmptyString(path)) {
            text += '; path=' + path;
        }

        // secure
        if (secure) {
            text += '; secure';
        }

        doc.cookie = name + '=' + text;
    };

    /**
     * 登录判断
     * @returns {boolean}
     */
    util.isLogin = function () {
        return !!util.getCookie(Z.cookieName);
    };

    /**
     * 从cookie里获取用户信息（登录名|用户类型）
     * @returns { object { name,type }}
     */
    util.getUserInfo = function () {
        var cookie = util.getCookie(Z.cookieName);
        if (!util.isLogin()) {
            return null;
        }
        var c = cookie.split('|');
        return {name: c[0], type: c[1]};
    };

    /**
     * 获取随机数
     * @returns {number}
     */
    util.getTimestamp = function () {
        return Math.round(new Date().getTime() / 1000);
    };

    /**
     * 取消事件默认行为
     * @param {Object} event
     */
    util.preventDefault = function (event) {
        if (event.preventDefault) {
            event.preventDefault();
        } else {
            event.returnValue = false;
        }
    };

    /**
     * 取消事件向上冒泡
     * @param {Object} event
     */
    util.stopPropagation = function (event) {
        if (event.stopPropagation) {
            event.stopPropagation();
        } else {
            event.cancelBubble = true;
        }
    };

    /**
     * 获取浏览器UA
     * @return {String}
     */
    util.getUA = function () {
        return win.navigator && win.navigator.userAgent || '';
    };

    /**
     * 是否IE6浏览器
     * @return {Boolean}
     */
    util.isIE6 = function () {
        var ua = util.getUA(),
            m = ua.match(/MSIE ([\w.]+)/);

        return !!(m && m[1] === '6.0');
    };

    /**
     * 火狐内核浏览器
     * @type {boolean}
     */
    util.isMozilla = function () {
        return /firefox/.test(navigator.userAgent.toLowerCase());
    };

    /**
     * 谷歌内核浏览器
     * @type {boolean}
     */
    util.isWebkit = function () {
        return /webkit/.test(navigator.userAgent.toLowerCase());
    };

    /**
     * IE内核浏览器
     * @type {boolean}
     */
    util.isMsie = function () {
        return /msie/.test(navigator.userAgent.toLowerCase());
    };

    /**
     * 是否移动终端
     * @return {Boolean}
     */
    util.isMobile = function () {
        var ua = util.getUA();
        return !!ua.match(/AppleWebKit.*Mobile.*/) || 'ontouchstart' in doc.documentElement;
    };

}(YZW, jQuery);

/**
 * @fileOverview 顶部登录状态信息条类
 * @author 老徐 <surging2@qq.com>
 * @version 0.1
 */
!function (Z, $) {
    var topbar = Z.topbar,
        domain = Z.domain,
        util = Z.util;

    /**
     * 顶通模板html
     * @type {{left: left, right: right}}
     */
    topbar.tpl = (function () {
        return {
            /**
             * 顶通左侧html
             * @returns {string}
             */
            left: function () {
                var tpl_arr = [],
                    domain_login = domain('domain_login'),
                    domain_reg = domain('domain_reg');

                tpl_arr.push('<p class="loginInfo">');

                //已登录状态
                if (util.isLogin()) {
                    var userInfo = util.getUserInfo();
					if(userInfo.type == '1'){
						domain_user = domain('domain_www');
					}else if(userInfo.type == '2'){
						domain_user = domain('domain_user');
					}
                    tpl_arr.push('您好，<a title="进入后台" href="' + domain_user + '?time=' + util.getTimestamp() + '">' + userInfo.name + '</a> ');

                    tpl_arr.push(' <a href="', domain_login, 'logout/?to=', window.location.href, '" target="_self">[ 退出 ]</a></p>');
                } else {
                    tpl_arr.push('您好，欢迎来到众用助手！请 [<a target="_self" href="' + domain_login + '">登录</a>] [<a target="_self" href="' + domain_reg + '">免费注册</a>]');
                }
                tpl_arr.push('</p>');
                return tpl_arr.join('');
            },
            /**
             * 顶通右侧html
             * @returns {string}
             */
            right: function () {
                var tpl_arr = [],
                    userInfo = util.getUserInfo(),
                    domain_zhs_www = domain('domain_zhs_www'),
                    domain_user = domain('domain_user'),
                    domain_list = domain('domain_list'),
                    domain_report = domain('domain_report'),
                    domain_hlzf = domain('domain_hlzf'),
                    domain_zhs_list = domain('domain_zhs_list'),
				  domain_sk_www = domain('domain_sk_www'),
				  domain_help = domain('domain_help'),
				  domain_www = domain('domain_www');

                tpl_arr.push('<div class="fr">');

                tpl_arr.push('<div class="top-nav-other">');
                if (util.isLogin()) {
					if(userInfo.type == 2 || userInfo.type == 4){
						tpl_arr.push('<a href="'+ domain_user + '">开发者中心</a>');
					}
                    tpl_arr.push('<a href="' + domain_www + '?time=' + util.getTimestamp() + '">众用助手首页</a>');   
                }
				
                tpl_arr.push('<a href="'+ domain_zhs_www + '">众划算</a>');
				tpl_arr.push('<a href="' + domain_sk_www + '">试客联盟</a>');
                tpl_arr.push('<a href="' + domain_hlzf + '">互联支付</a>');
                tpl_arr.push('<a href="'+ domain_help +'">帮助中心</a>');
                tpl_arr.push('</div>');
                return tpl_arr.join('');
            }
        };
    })();

    /**
     * 初始化
     */
    topbar.init = function () {
        topbar.renderUI();
        topbar.bindEvent();
    };

    /**
     * 获取用户未读提醒
     */
    topbar.getTipMesNum = (function () {
        return {
            ajaxGetMes: function () {
                if (!util.isLogin())return;
                $.ajax({
                    type: 'get',
                    dataType: 'json',
                    url: domain('domain_www') + 'get_stat/?type=msg&callback=?',
                    success: function (data) {
                        if (!data.error && data.msg_unread > 0) {
                            return data.msg_unread;
                        }
                    }
                });
            }
        };
    })();

    /**
     * 事件绑定
     */
    topbar.bindEvent = function () {
        //移动终端点击触发
        if (util.isMobile()) {
            $('#J_TopNavManuHover').on('click', function () {
                $(this).toggleClass('top-nm-hover');
            });
        } else {
            $('#J_TopNavManuHover').hover(function () {
                $(this).addClass('top-nm-hover');
            }, function () {
                $(this).removeClass('top-nm-hover');
            });
        }
    };

    /**
     * 拼装 UI
     */
    topbar.renderUI = function () {
        var tpl = topbar.tpl;

        $('#topbar').html('<div class="mod-wrap-index top_nav">' + tpl.left() + tpl.right() + '</div>');
    };

    topbar.init();
}(YZW, jQuery);

